home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / aros / dos / dos_init.c < prev    next >
C/C++ Source or Header  |  1996-09-13  |  6KB  |  221 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: dos_init.c,v 1.6 1996/09/13 17:50:06 digulla Exp $
  4.     $Log: dos_init.c,v $
  5.     Revision 1.6  1996/09/13 17:50:06  digulla
  6.     Use IPTR
  7.  
  8.     Revision 1.5  1996/09/12 14:52:03  digulla
  9.     Use correct way to access external names (was missing)
  10.  
  11.     Revision 1.4  1996/09/11 16:54:23  digulla
  12.     Always use __AROS_SLIB_ENTRY() to access shared external symbols, because
  13.     some systems name an external symbol "x" as "_x" and others as "x".
  14.     (The problem arises with assembler symbols which might differ)
  15.  
  16.     Revision 1.3  1996/08/13 13:52:53  digulla
  17.     Replaced <dos/dosextens.h> by "dos_intern.h" or added "dos_intern.h"
  18.     Replaced __AROS_LA by __AROS_LHA
  19.  
  20.     Revision 1.2  1996/08/01 17:40:49  digulla
  21.     Added standard header for all files
  22.  
  23.     Desc:
  24.     Lang:
  25. */
  26. #include <exec/types.h>
  27. #include <exec/resident.h>
  28. #include <exec/execbase.h>
  29. #include <clib/exec_protos.h>
  30. #include <aros/libcall.h>
  31. #include <dos/dosextens.h>
  32. #include <dos/dostags.h>
  33. #include <clib/dos_protos.h>
  34. #include <utility/tagitem.h>
  35. #include "dos_intern.h"
  36.  
  37. static const char name[];
  38. static const char version[];
  39. static const APTR Dos_inittabl[4];
  40. static void *const Dos_functable[];
  41. struct DosLibrary *__AROS_SLIB_ENTRY(init,Dos) ();
  42. extern const char Dos_end;
  43.  
  44. int Dos_entry(void)
  45. {
  46.     /* If the library was executed by accident return error code. */
  47.     return -1;
  48. }
  49.  
  50. /* CreateNewProc needs a global DOSBase variable */
  51. struct DosLibrary *DOSBase;
  52.  
  53. const struct Resident Dos_resident=
  54. {
  55.     RTC_MATCHWORD,
  56.     (struct Resident *)&Dos_resident,
  57.     (APTR)&Dos_end,
  58.     RTF_AUTOINIT,
  59.     39,
  60.     NT_LIBRARY,
  61.     0,
  62.     (char *)name,
  63.     (char *)&version[6],
  64.     (ULONG *)Dos_inittabl
  65. };
  66.  
  67. static const char name[]="dos.library";
  68.  
  69. static const char version[]="$VER: dos 39.0 (28.3.96)\n\015";
  70.  
  71. static const APTR Dos_inittabl[4]=
  72. {
  73.     (APTR)sizeof(struct DosLibrary),
  74.     (APTR)Dos_functable,
  75.     NULL,
  76.     &__AROS_SLIB_ENTRY(init,Dos)
  77. };
  78.  
  79. void LDDemon();
  80. void __AROS_SLIB_ENTRY(OpenLibrary,Dos)();
  81. void __AROS_SLIB_ENTRY(OpenDevice,Dos)();
  82. void __AROS_SLIB_ENTRY(CloseLibrary,Dos)();
  83. void __AROS_SLIB_ENTRY(CloseDevice,Dos)();
  84. void __AROS_SLIB_ENTRY(RemLibrary,Dos)();
  85. void LDFlush();
  86.  
  87. __AROS_LH2(struct DosLibrary *, init,
  88.  __AROS_LHA(struct DosLibrary *, dosBase, D0),
  89.  __AROS_LHA(BPTR,               segList,   A0),
  90.        struct ExecBase *, sysBase, 0, Dos)
  91. {
  92.     __AROS_FUNC_INIT
  93.     /* This function is single-threaded by exec by calling Forbid. */
  94.  
  95.     /* Store arguments */
  96.     DOSBase=dosBase;
  97.     dosBase->dl_SysBase=sysBase;
  98.     dosBase->dl_SegList=segList;
  99.  
  100.     InitSemaphore(&dosBase->dl_DosListLock);
  101.     InitSemaphore(&dosBase->dl_LDSigSem);
  102.  
  103.     dosBase->dl_UtilityBase=OpenLibrary("utility.library",39);
  104.     if(dosBase->dl_UtilityBase!=NULL)
  105.     {
  106.     static const struct TagItem tags[]=
  107.     {
  108.         { NP_Entry, (IPTR)LDDemon },
  109.         { NP_Input, 0 },
  110.         { NP_Output, 0 },
  111.         { NP_Name, (IPTR)"lib & dev loader demon" },
  112.         { TAG_END, 0 }
  113.     };
  114.     dosBase->dl_LDDemon=CreateNewProc((struct TagItem *)tags);
  115.     if(dosBase->dl_LDDemon!=NULL)
  116.     {
  117.         (void)SetFunction(&SysBase->LibNode,-92*sizeof(struct JumpVec),__AROS_SLIB_ENTRY(OpenLibrary,Dos));
  118.         (void)SetFunction(&SysBase->LibNode,-74*sizeof(struct JumpVec),__AROS_SLIB_ENTRY(OpenDevice,Dos));
  119.         (void)SetFunction(&SysBase->LibNode,-69*sizeof(struct JumpVec),__AROS_SLIB_ENTRY(CloseLibrary,Dos));
  120.         (void)SetFunction(&SysBase->LibNode,-75*sizeof(struct JumpVec),__AROS_SLIB_ENTRY(CloseDevice,Dos));
  121.         (void)SetFunction(&SysBase->LibNode,-67*sizeof(struct JumpVec),__AROS_SLIB_ENTRY(RemLibrary,Dos));
  122.         (void)SetFunction(&SysBase->LibNode,-73*sizeof(struct JumpVec),__AROS_SLIB_ENTRY(RemLibrary,Dos));
  123.         dosBase->dl_LDHandler.is_Node.ln_Name="lib & dev loader demon";
  124.         dosBase->dl_LDHandler.is_Node.ln_Pri=0;
  125.         dosBase->dl_LDHandler.is_Code=LDFlush;
  126.         AddMemHandler(&dosBase->dl_LDHandler);
  127.         return dosBase;
  128.     }
  129.     CloseLibrary(dosBase->dl_UtilityBase);
  130.     }
  131.  
  132.     return NULL;
  133.     __AROS_FUNC_EXIT
  134. }
  135.  
  136. __AROS_LH1(struct DosLibrary *, open,
  137.  __AROS_LHA(ULONG, version, D0),
  138.        struct DosLibrary *, DOSBase, 1, Dos)
  139. {
  140.     __AROS_FUNC_INIT
  141.     /*
  142.     This function is single-threaded by exec by calling Forbid.
  143.     If you break the Forbid() another task may enter this function
  144.     at the same time. Take care.
  145.     */
  146.  
  147.     /* Keep compiler happy */
  148.     version=0;
  149.  
  150.     /* I have one more opener. */
  151.     DOSBase->dl_lib.lib_OpenCnt++;
  152.     DOSBase->dl_lib.lib_Flags&=~LIBF_DELEXP;
  153.  
  154.     /* You would return NULL if the open failed. */
  155.     return DOSBase;
  156.     __AROS_FUNC_EXIT
  157. }
  158.  
  159. __AROS_LH0(BPTR, close,
  160.        struct DosLibrary *, DOSBase, 2, Dos)
  161. {
  162.     __AROS_FUNC_INIT
  163.     /*
  164.     This function is single-threaded by exec by calling Forbid.
  165.     If you break the Forbid() another task may enter this function
  166.     at the same time. Take care.
  167.     */
  168.  
  169.     /* I have one fewer opener. */
  170.     if(!--DOSBase->dl_lib.lib_OpenCnt)
  171.     {
  172.     /* Delayed expunge pending? */
  173.     if(DOSBase->dl_lib.lib_Flags&LIBF_DELEXP)
  174.         /* Then expunge the library */
  175.         return expunge();
  176.     }
  177.     return 0;
  178.     __AROS_FUNC_EXIT
  179. }
  180.  
  181. __AROS_LH0(BPTR, expunge,
  182.        struct DosLibrary *, DOSBase, 3, Dos)
  183. {
  184.     __AROS_FUNC_INIT
  185.  
  186.     BPTR ret;
  187.     /*
  188.     This function is single-threaded by exec by calling Forbid.
  189.     Never break the Forbid() or strange things might happen.
  190.     */
  191.  
  192.     /* Test for openers. */
  193.     if(DOSBase->dl_lib.lib_OpenCnt)
  194.     {
  195.     /* Set the delayed expunge flag and return. */
  196.     DOSBase->dl_lib.lib_Flags|=LIBF_DELEXP;
  197.     return 0;
  198.     }
  199.  
  200.     /* Get rid of the library. Remove it from the list. */
  201.     Remove(&DOSBase->dl_lib.lib_Node);
  202.  
  203.     /* Get returncode here - FreeMem() will destroy the field. */
  204.     ret=DOSBase->dl_SegList;
  205.  
  206.     /* Free the memory. */
  207.     FreeMem((char *)DOSBase-DOSBase->dl_lib.lib_NegSize,
  208.         DOSBase->dl_lib.lib_NegSize+DOSBase->dl_lib.lib_PosSize);
  209.  
  210.     return ret;
  211.     __AROS_FUNC_EXIT
  212. }
  213.  
  214. __AROS_LH0I(int, null,
  215.         struct DosLibrary *, DOSBase, 4, Dos)
  216. {
  217.     __AROS_FUNC_INIT
  218.     return 0;
  219.     __AROS_FUNC_EXIT
  220. }
  221.